﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Input;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
    public class IPadViewItem : Control
    {
        public Point OldLocation = new Point(0, 0);
        public Storyboard MoveStoryboard;
        //存储当前鼠标位置
        private Point MouseXY;
        ////防止重复左右移动
        private bool IsMove = false;
        /// <summary>
        /// 是否可移动标签
        /// </summary>
        public bool ChangedMove = false;
        /// <summary>
        /// 移动后标签的新下标，用于重新排序位置
        /// </summary>
        [Description("移动后标签的新下标，用于重新排序位置")]
        public int NewTabPageIndex
        {
            get { return GetValue<int>(); }
            set { SetValue(value); }
        }
        public IPadViewControl HostTabBarControl;
        //模板定义
        protected override void InitializeComponent()
        {
            ClipToBounds = true;
        }
        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Focus();
                Mouxy = e.Location;
                isclick = true;
                CaptureMouse();
                //ZIndex = Parent.GetChildren();
            }
            if (HostTabBarControl == null)
            {
                return;
            }
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                MouseXY = new Point(-e.Location.X, -e.Location.Y);
            }
        }
        private Point Mouxy;
        private bool isclick = false;
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.LeftButton == MouseButtonState.Pressed && isclick)
            {
                    var a = e.Location - Mouxy;
                    // TransformPoint
                if (HostTabBarControl == null)
                {
                    return;
                }
                if (!HostTabBarControl.ChangedMove)
                {
                    return;
                }

                IPadViewItem changedTabItem = HostTabBarControl.FindMoveItemToItem(this);
                if (changedTabItem != null&& !changedTabItem.IsMove)
                {
                    int index = changedTabItem.NewTabPageIndex;
                    changedTabItem.NewTabPageIndex = NewTabPageIndex;
                    NewTabPageIndex = index;
                    changedTabItem.IsMove= true;
                    Point move = this.OldLocation;
                    this.OldLocation = changedTabItem.OldLocation;
                    changedTabItem.OldLocation = move;
                    changedTabItem.Move();
                }
                //Point p = new Point(MarginLeft.Value, MarginTop.Value);
                //p.Offset(MouseXY.X, MouseXY.Y);
                //Point nowp = HostTabBarControl.PointToClient(p);
                MarginTop += a.Y;
                MarginLeft += a.X;

            }
        }
        public void Move()
        {
            var storyboard = new Storyboard
                {
                    Timelines =
                {
                    new Timeline(1)
                    {
                        KeyFrames =
                        {
                            new KeyFrame<FloatField> { Property = nameof(Control.MarginTop), Value =OldLocation.Y},
                            new KeyFrame<FloatField> { Property = nameof(Control.MarginLeft), Value =OldLocation.X},
                        },
                    }
                }
                };
            storyboard.Start(this, TimeSpan.FromSeconds(0.1), 1, EndBehavior.Reservations);
            storyboard.Completed += (s, e) =>
            {
                this.IsMove=false;
            };
        }
        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);
            if (e.LeftButton == MouseButtonState.Released)
            {
                isclick = false;
                ReleaseMouseCapture();
            }
            if (HostTabBarControl == null)
            {
                return;
            }
            if (!HostTabBarControl.ChangedMove)
            {
                return;
            }
            Move();
        }
#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();

        }
        //用户代码

#endif
    }
}
